#!/bin/bash

API_BASE="http://localhost:3002/api/v1"

echo "🔧 Testing Admin Functions"
echo ""

# 1. 登录获取管理员token
echo "1. Login as admin (testdev)"
LOGIN_RESPONSE=$(curl -s -X POST "$API_BASE/auth/login" \
  -H "Content-Type: application/json" \
  -d '{
    "email": "testdev@example.com",
    "password": "password123"
  }')

# 提取token
TOKEN=$(echo "$LOGIN_RESPONSE" | grep -o '"accessToken":"[^"]*"' | cut -d'"' -f4)

if [ -z "$TOKEN" ]; then
  echo "❌ Admin login failed"
  echo "Response: $LOGIN_RESPONSE"
  exit 1
fi

echo "✅ Admin login successful"
echo ""

# 2. 测试系统统计
echo "2. Get system statistics"
STATS_RESPONSE=$(curl -s -X GET "$API_BASE/admin/stats" \
  -H "Authorization: Bearer $TOKEN")
echo "Response: $STATS_RESPONSE"
echo ""

# 3. 测试获取待审核插件
echo "3. Get pending plugins"
PENDING_RESPONSE=$(curl -s -X GET "$API_BASE/admin/plugins/pending" \
  -H "Authorization: Bearer $TOKEN")
echo "Response: $PENDING_RESPONSE"
echo ""

# 4. 测试获取用户列表
echo "4. Get users list"
USERS_RESPONSE=$(curl -s -X GET "$API_BASE/admin/users" \
  -H "Authorization: Bearer $TOKEN")
echo "Response: $USERS_RESPONSE"
echo ""

# 5. 测试获取审计日志
echo "5. Get audit logs"
AUDIT_RESPONSE=$(curl -s -X GET "$API_BASE/admin/audit-logs" \
  -H "Authorization: Bearer $TOKEN")
echo "Response: $AUDIT_RESPONSE"
echo ""

echo "🎉 Admin function testing completed!"
